<template>
	<view class="content">
		<u-navbar :placeholder='true' title="添加分行/子公司" leftIcon="arrow-leftward"
			@leftClick="leftClick('/pages/mine/setting/childcompany/index')">
		</u-navbar>
		<view class="main">
			<view>
				<view class="form">
					<view class="form-l">
						所屬母公司
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<picker @change="bindPickerChange" :value="index" :range="array" range-key="branchname">
							<view class="picker">
								<view class="uni-input">{{from.parentname}}</view>
								<u-icon size="28rpx" name="arrow-right"></u-icon>
							</view>
						</picker>
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						分行/子公司編號
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="from.branch_code" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						名稱（中）
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="from.branchname" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						名稱（英）
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="from.en_branchname" class="input-item" type="text" placeholder="">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						聯繫人
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="from.principal" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						電話
						<text style="color:#ED0000 ;">*</text>
					</view>
					<view class="form-r">
						<input v-model="from.phone" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						郵箱
					</view>
					<view class="form-r">
						<input v-model="from.email" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						地址
					</view>
					<view class="form-r">
						<input v-model="from.address" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						傳真
					</view>
					<view class="form-r">
						<input v-model="from.fax" class="input-item" type="text">
					</view>
				</view>
				<view class="form">
					<view class="form-l">
						分配金額
					</view>
					<view class="form-r">
						<input v-model="from.amount" class="input-item" type="number">
					</view>
				</view>

			</view>
			<view style="text-align: center;color: #909399;" v-if="from.amount && from.parentname">
				剩餘可分配額度：{{balance-from.amount}}
			</view>
		</view>
		<view class="btn" @click="submit">
			提交
		</view>
	</view>
</template>

<script>
	import {
		getparent,
		addchildcompany,
		getamount
	} from '@/network/api/setting.js'
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				balance: '',
				array: [],
				index: 0,
				value: false,
				from: {
					parent_id: '',
					parentname: '',
					branch_id: '',
					branch_code: '',
					branchname: '',
					en_branchname: '',
					principal: '',
					phone: '',
					email: '',
					address: '',
					fax: '',
					amount: '',
				}
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
			})
		},
		onShow() {
			this.getParent()
		},
		methods: {
			async submit() {
				if (!this.from.parentname) {
					return uni.showToast({
						title: '所屬母公司不能为空',
						icon: 'none'
					})
				}
				if (!this.from.branch_code) {
					return uni.showToast({
						title: '分行/子公司編號不能为空',
						icon: 'none'
					})
				}
				if (!this.from.branchname) {
					return uni.showToast({
						title: '名稱（中）不能为空',
						icon: 'none'
					})
				}
				if (!this.from.en_branchname) {
					return uni.showToast({
						title: '名稱（英）不能为空',
						icon: 'none'
					})
				}
				if (!this.from.principal) {
					return uni.showToast({
						title: '聯繫人不能为空',
						icon: 'none'
					})
				}
				if (!this.from.phone) {
					return uni.showToast({
						title: '電話不能为空',
						icon: 'none'
					})
				}
				const res = await addchildcompany(this.from)
				if (res.data.code == 1) {
					uni.showToast({
						title: '添加成功',
					})
          setTimeout(() => {
            uni.navigateBack({
              delta: 1
            })
          }, 1000)
				}
				if (res.data.code == 2) {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					})
				}
			},
			async getParent() {
				let data = {
					company_id: this.userinfo.company_id
				}
				const res = await getparent(data)
				this.array = res.data.branch
			},
			async bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.index = e.detail.value
				this.from.parentname = this.array[this.index].branchname
				this.from.parent_id = this.array[this.index].branch_id
				let data = {
					branch_id: this.array[this.index].branch_id
				}
				const res = await getamount(data)
				this.balance = res.data.balance
			},
			leftClick(url) {
				console.log('rightClick');
				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #F5F6F9;
		width: 100%;
		height: 100vh;
		position: relative;

		.main {
			background-color: #fff;

			.form {
				margin: 0 32rpx;
				padding: 32rpx 0;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid #DEDEDE;
				;

				.form-l {
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}

				.form-r {

					// flex-direction: row-reverse;
					// margin-left: 48rpx;
					// flex: 1;
					// float: right;
					.picker {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.uni-input {
							font-size: 32rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #BEBEBE;
						}
					}

				}
			}

			.form:last-child {
				border-bottom: none;
			}
		}

		.open {
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
			margin-top: 22rpx;
			background-color: #fff;
			padding: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.btn {
			position: absolute;
			bottom: 15rpx;
			left: 50%;
			transform: translateX(-50%);
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: bold;
			line-height: 80rpx;
			color: #FFFFFF;
			width: 686rpx;
			height: 80rpx;
			text-align: center;
			background: #645AED;
			opacity: 1;
			border-radius: 30rpx;
		}
	}
</style>
